// pages/coin/index.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSign:false,
    active:-1,
    speed: 50,
    cycle: 0,
    dflag:true,
    awardArr:[
      { 'pic': '/assets/images/draw/coin.png', 'name': '' },
      { 'pic': '/assets/images/draw/quan.png', 'name': '' },
      { 'pic': '/assets/images/draw/coin.png', 'name': '' },
      { 'pic': '/assets/images/draw/quan.png', 'name': '' },
      { 'pic': '/assets/images/draw/coin.png', 'name': '' },
      { 'pic': '/assets/images/draw/quan.png', 'name': '' },
      { 'pic': '/assets/images/draw/coin.png', 'name': '' },
      { 'pic': '/assets/images/draw/quan.png', 'name': '' },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init:function(){
    var that = this;
    var data = {
      customerId: app.globalData.userInfo['ID']
    }
    request.request('api/Customer/HasSignInToday', data).then(function(res){

      console.log(res);

      if(res.data.status == 1){
        that.setData({
          "isSign": true
        })
      }
    });

    that.drawInit();
  },
  // 抽奖初始化
  drawInit(){
    var that = this;
    that.setData({
      active: -1,
      speed:50,
      cycle: 0,
      dflag: true
    })
  },
  draw: function () {
    console.log(this.dflag);
    if (this.data.dflag) {
      this.roll();
      this.setData({
        dflag: false
      })
    }
  },

  roll: function () {

    var that = this;
    var speed = that.data.speed;
    setTimeout(function () {
      var active = that.data.active;
      var cycle = that.data.cycle;
      var speed = that.data.speed;
      active++;
      if (active == 8) {
        cycle++;
        active = 0;

        speed = 80 * cycle;

        if (cycle > 4) {
          speed = 150 * cycle;
        }

      }

      //转两圈获取中将圈数
      if (cycle == 5) {
        //设置中将索引
        if (active == 7) {
          cycle = 99;
          wx.showToast({
            title: '恭喜中奖',
            icon: 'success',
            duration: 2000
          })
          that.drawInit();
        }
      }

      if (cycle <= 5) {
        that.setData({
          active: active,
          cycle: cycle,
          speed: speed
        })
        console.log("第几圈：" + cycle + "。转速：" + speed);
        that.roll();
      }
    }, speed);
  },
  //立即签到
  signBtn:function(){

    var data = {
      CustomerID: app.globalData.userInfo['ID']
    }

    request.request('api/Customer/ToSignIn', data,'post').then(function (res) {
      console.log(res);
    })
  }
})